import cv2 as cv
import time
from pathlib import Path
import requests
import os
class catch_img():

    def face_data(self):
        pass


    def __init__(self,name):
        self.name = name

        self.url = 'https://image.baidu.com/search/acjson?'  # 百度图片链接
        self.headers = {     ## 设置请求头

                'Cookie': 'BDqhfp=%E7%8B%97%26%260-10-1undefined%26%260%26%261; BIDUPSID=E5B9CDED7071EEEE2A89CAB9C2E11AD7; PSTM=1623558395; BAIDUID=FAC888944227661413FCC9BC4AB0F78F:FG=1; __yjs_duid=1_18022e2bb8011c006a066cea2beedb2f1626079007527; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; BAIDUID_BFESS=FAC888944227661413FCC9BC4AB0F78F:FG=1; BDRCVFR[FIXrT5n2Tgt]=mk3SLVN4HKm; H_PS_PSSID=31660_26350; BA_HECTOR=212481agakag0505291ggch9l0q; BDRCVFR[X_XKQks0S63]=mk3SLVN4HKm; userFrom=www.baidu.com; firstShowTip=1; indexPageSugList=%5B%22%E7%8B%97%22%5D; cleanHistoryStatus=0; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; ab_sr=1.0.1_YWM1NDBjODBkMzFhYzNiMTc1ZmIzYjY1YjUyOTQ5ZjBkZjg0MDQ0MWQyMDY2YmIzMzVlNTZmMDk5Zjg5MzAxYjBlNjVlNDU3ODZkNjQwZWIxMDNjNjgzYjAyNWZkYmEyZTQ3NTNlYTZlOWE4MGZhMWY0MmUwNzBjYmRiNzQ4NzNiNWM3YmMyZmM0OGNjMzYzNmU2ZjEzYWM0MTBkODhkYw==; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm',
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'
            }
        self.params = {   #   请求头的参数
                'tn': 'resultjson_com',
                'logid': '10121964150891701915',
                'ipn': 'rj',
                'ct': '201326592',
                'is': '',
                'fp': 'result',
                'queryWord': '狗',
                'cl': '2',
                'lm': '-1',
                'ie': 'utf-8',
                'oe': 'utf-8',
                'adpicid': '',
                'st': '-1',
                'z': '',
                'ic': '0',
                'hd': '',
                'latest': '',
                'copyright': '',
                'word': '{}'.format(name),  ## 搜索的名字
                's': '',
                'se': '',
                'tab': '',
                'width': '',
                'height': '',
                'face': '0',
                'istype': '2',
                'qc': '',
                'nc': '1',
                'fr': '',
                'expermode': '',
                'nojc': '',
                'pn': '',
                'rn': '30',
                'gsm': '',
                'time': ''
            }
        self.img_list = []  ## 图片链接列表

        if not os.path.exists("./img/{}".format(name)):
            os.mkdir("./img/{}".format(name))
    def k_num(self,k,m):
        k=m
    def get_img(self, num,file_name,K_num):
        """
            :param self:
            :param num:  爬取图片的数量n*30
            :param file_name:  保存的路径

            :return:
        """
        for i in range(0, num):
            self.params['time'] = int(time.time() * 1000)
            self.params['pn'] = i * 30
            response = requests.get(url=self.url, headers=self.headers, params=self.params)
            for j in range(0, len(response.json()['data']) - 1):
                self.img_list.append(response.json()['data'][j]['thumbURL'])

        n = 0
        K_num[0] = 0
        if not Path(file_name):   #### 如果路径不存在返回 False
            return False
        for j in self.img_list:
            image = requests.get(url=j)
            with open('{0}/{1}/{2}.jpg'.format(file_name,self.name,n), 'wb') as f:
                f.write(image.content)

            # print("爬取第{}张图片".format(n))
            n += 1
            K_num[0] = K_num[0] + 1
            print("k={0}  n ={1}".format(K_num[0],n))
            time.sleep(0.1)
        return  0





# def run():
# #
# z = [0]
# img = catch_img("哈士奇")  #名字
# img.get_img(1,"./img",z)  #n*30张
#
# print(z)